package com.mbyte.easy.sign.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.sign.entity.Sign;
import com.mbyte.easy.sign.service.ISignService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/sign/sign")
public class SignController extends BaseController  {

    private String prefix = "sign/sign/";

    @Autowired
    private ISignService signService;
    @Autowired
    private IQcbyClassService qcbyClassService;
	//AF

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param sign
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,String createTimeSpace,Sign sign) {
        System.out.println("接受的"+sign.getStimeSpace());
        Page<Sign> page = new Page<Sign>(pageNo, pageSize);
        QueryWrapper<Sign> queryWrapper = new QueryWrapper<Sign>();
//        if(!ObjectUtils.isEmpty(sign.getUserId())) {
//            queryWrapper = queryWrapper.like("user_id",sign.getUserId());
//         }
        if(!ObjectUtils.isEmpty(sign.getClassId())) {
            queryWrapper = queryWrapper.like("class_id",sign.getClassId());
         }
        if(!ObjectUtils.isEmpty(sign.getSignStatus())) {
            queryWrapper = queryWrapper.like("sign_status",sign.getSignStatus());
         }
        if(createTimeSpace != null){
            if (sign.getStimeSpace() != null){
                if (sign.getStimeSpace().equals("0")){
                    System.out.println("0");
                    queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.AMSTARTTIME,createTimeSpace+Sign.AMOVERTIME);
                }else if(sign.getStimeSpace().equals("1")){
                    System.out.println("1");
                    queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.PMSTARTTIME,createTimeSpace+Sign.PMOVERTIME);
                }else if(sign.getStimeSpace().equals("2")){
                    System.out.println("2");
                    queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.NIGHTSTARTTIME,createTimeSpace+Sign.NIGHTOVERTIME);
                }else if(sign.getStimeSpace().equals("3")){
                    System.out.println("3");
                    queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.ALLDAYSTARTTIME,createTimeSpace+Sign.ALLDAYOVERTIME);
                }
            }else {
                //默认查一整天
                sign.setStimeSpace("3") ;
                queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.ALLDAYSTARTTIME,createTimeSpace+Sign.ALLDAYOVERTIME);
            }
        }else {
            //默认查当天的打卡记录
            createTimeSpace = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
            sign.setStimeSpace("3");
            queryWrapper = queryWrapper.between("create_time",createTimeSpace+Sign.ALLDAYSTARTTIME,createTimeSpace+Sign.ALLDAYOVERTIME);
        }

//        if(!ObjectUtils.isEmpty(sign.getSignStatus())) {
//
//            System.out.println("测试时间"+sign.getCreateTime());
//            queryWrapper = queryWrapper.like("creat_time",sign.getCreateTime());
//        }
//        if(!ObjectUtils.isEmpty(sign.getDeleteTime())) {
//            queryWrapper = queryWrapper.like("delete_time",sign.getDeleteTime());
//         }
//        if(!ObjectUtils.isEmpty(sign.getDeleteStatus())) {
//            queryWrapper = queryWrapper.like("delete_status",sign.getDeleteStatus());
//         }
        IPage<Sign> pageInfo = signService.pageAddName(page, queryWrapper);
        model.addAttribute("createTimeSpace", createTimeSpace);
        System.out.println("timeSpace"+sign.getStimeSpace());
        model.addAttribute("searchInfo", sign);
        //（wq2020.6.16）
        model.addAttribute("normalClass", qcbyClassService.listNormal());
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param sign
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Sign sign){
        return toAjax(signService.save(sign));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("sign",signService.addNameAndClassName(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param sign
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Sign sign){
        return toAjax(signService.updateById(sign));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(signService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(signService.removeByIds(ids));
    }

}

